/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.wizard;

import java.awt.CardLayout;
import java.util.Vector;

/**
 *
 * @author jmj
 */
public class WizardSequenceController {

    private Vector<PageController> pages;
    private PageController currentPage;
    private CardLayout layout;
    private WizardView view;

    public WizardSequenceController(Vector<PageController> pages, WizardView view) {
        this.pages = pages;
        this.layout = (CardLayout) view.getjPnlPages().getLayout();
        this.view = view;
    }

    public PageController nextPage() {
        if ((this.currentPage != null) && (this.currentPage.hasNext())) {
            this.currentPage = this.pages.get(this.pages.indexOf(this.currentPage) + 1);
            this.showCurrentPage();
        }
        return this.currentPage;
    }

    public PageController previousPage() {
        if ((this.currentPage != null) && (this.currentPage.hasLast())) {
            this.layout.previous(this.view.getjPnlPages());
            this.currentPage = this.pages.get(this.pages.indexOf(this.currentPage) - 1);
            this.showCurrentPage();
        }
        return this.currentPage;
    }

    private void showCurrentPage() {
        if (!this.currentPage.pageInitialized()) {
            this.view.getjPnlPages().add(this.currentPage.getPage(), this.currentPage.getKey());
        }
        this.layout.show(this.view.getjPnlPages(), this.currentPage.getKey());
        this.view.getjBtnBack().setVisible(this.currentPage.hasLast());
        this.view.getjBtnNext().setVisible(this.currentPage.hasNext());
        this.view.getjBtnFinish().setVisible(this.currentPage.canDoFinish());
    }

    public PageController startSequence() {
        this.currentPage = this.pages.firstElement();
        this.showCurrentPage();
        return this.currentPage;
    }
}
